package com.shangyitong.yygh_cmn.controller.api;

import com.shangyitong.Result;
import com.shangyitong.yygh.model.cmn.Dict;
import com.shangyitong.yygh_cmn.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/cmn/dict")
@Slf4j
public class ApiDictController {

    @Autowired
    private DictService dictService;

    /**
     * 根据dictcode查询所有子节点
     * finished
     * @param dictCode
     * @return
     */
    @ApiOperation(value = "根据dictcode查询所有子节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        if (dictList != null) {
            return Result.ok(dictList);
        }
        return Result.fail();
    }

    /**
     * 根据parentId查询dict
     * finished
     * @param parentId
     * @return
     */
    @ApiOperation(value = "根据parent_id查询")
    @GetMapping("findByParentId/{parentId}")
    public Result findByParentId(@PathVariable Long parentId) {
        List<Dict> dicts = dictService.findChildData(parentId);
        if (dicts != null) {
            return Result.ok(dicts);
        }
        return Result.fail();
    }
}
